﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion

namespace KQReport.Utils
{
    public static class ListHelper
    {
        public static void Redundant<T>(ref List<T> list)
        {
            if (list == null)
                return;
            for (var i = list.Count - 1; i >= 0; i--)
                for (var j = list.Count - 1; j > i; j--)
                    if (list[i] == null && list[j] == null || list[i].Equals(list[j]))
                        list.RemoveAt(j);
        }

        /// <summary>
        ///   显示List
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="items"> </param>
        /// <param name="splitChar"> </param>
        /// <returns> </returns>
        public static string ListGetDisplay<T>(this IEnumerable<T> items, char splitChar = ',')
        {
            if (items == null||items.All(p=>p==null)) return null;
            var builder = new StringBuilder();
            foreach (var item in items)
            {
                builder.Append(item);
                builder.Append(splitChar);
            }
            if (builder.Length == 0) return null;
            var @string = builder.ToString();
            return @string.Substring(0, @string.Length - 1);
        }

        /// <summary>
        ///   List转换为文本
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="items"> </param>
        /// <param name="splitChars"> </param>
        /// <param name="func"> 格式化每个行的方法 </param>
        /// <returns> </returns>
        public static string ListGetDisplay<T>(this IEnumerable<T> items, Func<T, string> func, params char[] splitChars)
        {
            if (items == null || items.All(p => p == null)) return null;
            var builder = new StringBuilder();
            foreach (var item in items)
            {
                if (func != null)
                    builder.Append(func(item));
                else
                    builder.Append(item);
                foreach (var splitChar in splitChars)
                    builder.Append(splitChar);
            }
            if (builder.Length == 0) return null;
            var @string = builder.ToString();
            return @string.Substring(0, @string.Length - 1);
        }

        /// <summary>
        ///   文本转换为List
        /// </summary>
        /// <param name="str"> </param>
        /// <param name="splitChar"></param>
        /// <returns> </returns>
        public static string[] ListFromDisplay(this string str, char splitChar = ',')
        {
            if (string.IsNullOrEmpty(str))
                return null;
            str = str.Trim();
            var array = str.Split(new[] { splitChar }, StringSplitOptions.RemoveEmptyEntries);
            return array.Length == 0 ? new string[0] : array.ToArray();
        }

        /// <summary>
        ///   文本转换为List
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="str"> </param>
        /// <param name="func"> </param>
        /// <param name="splitChars"> </param>
        /// <returns> </returns>
        public static T[] ListFromDisplay<T>(this string str, Func<string, T> func, params char[] splitChars)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            str = str.Trim();
            var array = str.Split(splitChars.ToArray(), StringSplitOptions.RemoveEmptyEntries);
            if (array.Length == 0)
                return new T[0];
            return array.Select(func).ToArray();
        }
    }
}